<template>
  <div class="callServices">
    <x-header class="header" :left-options="{showBack: showHeaderBack,preventGoBack:true}" @on-click-back="goToPage" ref="header">呼叫服务</x-header>
    <div class="topHolder"></div>
    <card>
      <div class="card-padding" slot="content">
        <i class="iconfont icon-erweima2 barcode"></i>
        <group-title>找到房号（台号）扫一扫呼叫服务</group-title>
        <box gap="1rem 0.8rem">
          <x-button :gradients="['#1D62F0', '#19D5FD']" @click.native="scan" :show-loading="loadingStatus" :disabled="loadingStatus" text="扫一扫"></x-button>
        </box>
      </div>
    </card>
  </div>
</template>

<script>
  import { Box, Card, GroupTitle, XButton } from 'vux'
  import {getWeiXinConfig} from "../../api";
  import {message} from "../../common/util";

  export default {
    name:'callServices',
    data(){
      return{
        showHeaderBack:this.GLOBAL.showHeaderBack,
        loadingStatus:false
      }
    },
    components:{
      Box,
      Card,
      GroupTitle,
      XButton
    },
    computed:{

    },
    methods:{
      goToPage(){
        this.$router.push({path:'/'})
      },
      scan(){
        if(JSON.parse(localStorage.getItem('debugMode')))
          this.$router.push({name:'扫码操作',params:{tableId:'481cea253aa845c695e9dac4a97ae980'}})//写死测试用台的ID
        else
        this.$wechat.scanQRCode({
          needResult: 0, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
          scanType: ["qrCode"], // 可以指定扫二维码还是一维码，默认二者都有
          success: (res)=> {
            //console.log('scanQRCode success')
            //console.log(res.resultStr) // 当needResult 为 1 时，扫码返回的结果
          }
        });
      }
    },
    mounted(){
      return new Promise((resolve, reject)=>{
        if(!JSON.parse(localStorage.getItem('debugMode')) && (sessionStorage.getItem('weixinConfig') == null || !sessionStorage.getItem('weixinConfig'))){
          getWeiXinConfig(window.location.href).then((res)=>{
            if(res.status == 'success'){
              this.$wechat.config(res.data)
              console.log('getWeiXinConfig 2 in',res.data)
              sessionStorage.setItem('weixinConfig',true)
              resolve()
            }else if(res.status == 'error') {
              message({message:res.msg,type:'error'})
              console.log('getWeiXinConfig error info:', res.msg)
            }
          }).catch((err)=>{console.log('getWeiXinConfig catch info:',err)})
        }else{
          resolve()
        }
      }).then(()=>{
        this.loadingStatus = false
        this.$store.commit('updateLoadingStatus',{isLoading:false})
      })
    }
  }
</script>

<style lang="scss" scoped>
  .callServices{
    .card-padding{
      text-align: center;
      .barcode{font-size: 7rem;}
    }
  }
</style>
